package com.smallkingson.ylib.view;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.graphics.drawable.ColorDrawable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;

import com.smallkingson.ylib.core.Yr;
import com.smallkingson.ylib.view.base.YView;


public class SideBar extends YView {
	// 触摸事件
	private OnTouchingLetterChangedListener onTouchingLetterChangedListener;
	// 26个字母
	public static String[] b = { "A", "B", "C", "D", "E", "F", "G", "H",
			"I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U",
			"V", "W", "X", "Y", "Z" ,"#"};
	private int choose = -1;// 选中
	private Paint paint = new Paint();

	private TextView mTextDialog;
	private int singleHeight;
	private int height;
	private int width;
	private int hintBackground;
	private int textSize = 20;
	private int selColor = Color.parseColor("#3399ff");
	private int textColor = 0xFF717171;
	private Map<String, Integer> charToPositionMap;

	public SideBar setSelColor(int selColor) {
		this.selColor = selColor;
		postInvalidate();
		return this;
	}

	public SideBar setTextSize(int textSize) {
		this.textSize = textSize;
		postInvalidate();
		return this;
	}

	public SideBar setHintBackground(int hintBackground) {
		this.hintBackground = hintBackground;
		postInvalidate();
		return this;
	}

	public SideBar setTextView(TextView mTextDialog) {
		this.mTextDialog = mTextDialog;
		postInvalidate();
		return this;
	}

	public SideBar(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	public SideBar(Context context) {
		super(context);
	}

	@Override
	protected void onCreate() {

		height = getHeight();

		width = getWidth();

		singleHeight = height / b.length;
	}

	public void setTextColor(int textColor) {
		this.textColor = textColor;
	}

	/**
	 * 重写这个方法
	 */
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);

		for (int i = 0; i < b.length; i++) {

			paint.setColor(textColor);

			paint.setTypeface(Typeface.DEFAULT_BOLD);

			paint.setAntiAlias(true);

			paint.setTextSize(textSize);
			// 选中的状态
			if (i == choose) {

				paint.setColor(selColor);

				paint.setFakeBoldText(true);
			}
			// x坐标等于中间-字符串宽度的一半.
			float xPos = width / 2 - paint.measureText(b[i]) / 2;

			float yPos = singleHeight * i + singleHeight;

			canvas.drawText(b[i], xPos, yPos, paint);

			paint.reset();// 重置画笔
		}

	}

	@Override
	public boolean dispatchTouchEvent(MotionEvent event) {

		final int action = event.getAction();

		final float y = event.getY();// 点击y坐标

		final int oldChoose = choose;

		final int position = (int) (y / singleHeight);// 点击y坐标所占总高度的比例*b数组的长度就等于点击b中的个数.

		switch (action) {

		case MotionEvent.ACTION_UP: {

			Yr.d();
			setBackgroundDrawable(new ColorDrawable(0x00000000));
			choose = -1;//
			invalidate();
			if (mTextDialog != null) {

				mTextDialog.setVisibility(View.INVISIBLE);
			}
			break;
		}

		default: {
			if (hintBackground == 0) {

				setBackgroundColor(0x33333333);
			} else {

				setBackgroundResource(hintBackground);
			}
			if (oldChoose != position) {
				if (position >= 0 && position < b.length) {
					Yr.d("onTouchingLetterChangedListener : "+onTouchingLetterChangedListener +"    charToPositionMap=" +charToPositionMap);
					if (onTouchingLetterChangedListener != null&&charToPositionMap!=null) {
						String c = b[position];
						Integer p = charToPositionMap.get(c);
						if(p==null){
							p=-1;
						}
						onTouchingLetterChangedListener.onTouchingLetterChanged(b[position],  p);
					}
					if (mTextDialog != null) {
						mTextDialog.setText(b[position]);

						mTextDialog.setVisibility(View.VISIBLE);
					}

					choose = position;
					invalidate();
				}
			}

			break;
		}
		}
		return true;
	}

	/**
	 * 向外公开的方法
	 * 
	 * @param onTouchingLetterChangedListener
	 */
	public SideBar setOnTouchingLetterChangedListener(
			OnTouchingLetterChangedListener onTouchingLetterChangedListener) {
		this.onTouchingLetterChangedListener = onTouchingLetterChangedListener;
		return this;
	}

	/**
	 * 静态方法
	 */

	/**
	 * 获取字符第一次出现位置 的集合
	 * 
	 * @param list
	 * @return
	 */
	public static <T extends Sideable> Map<String, Integer> getCharToPositionMap(
			List<T> list) {

		if(Yr.isDebug()){
			StringBuffer sb=new StringBuffer();
			for(int i=0;i<list.size();i++){
				sb.append(list.get(i).getChar()+", ");
			}
			Yr.d(sb);
		}
		Map<String, Integer> map = new HashMap<String, Integer>();

		String lastChar =list.get(0).getChar().toUpperCase();

		map.put(lastChar, 0);

		for (int i = 0; i < list.size(); i++) {
			String c = list.get(i).getChar().toUpperCase();
			if (!lastChar.equals(c)) {
				Yr.d(c+":"+i);
				map.put(c , i);
				lastChar =c ;
			}
		}

		return map;
	}

	/**
	 * 字母首次出现的位置
	 * @param s
	 * @return
     */
	public int StringAt(String s){
		Integer integer = charToPositionMap.get(s.toUpperCase());
		return integer;

	}

	public <T extends Sideable> void initCharToPositionMap(List<T> list) {
		charToPositionMap = getCharToPositionMap(list);
	}

	/**
	 * 接口
	 * 
	 * @author coder
	 * 
	 */
	public static interface OnTouchingLetterChangedListener {
		public void onTouchingLetterChanged(String s, int position);
	}

	public static interface Sideable {
		String getChar();
	}
}